package com.qiyeji.real_estate.controller;

import com.qiyeji.real_estate.entity.timeTable;
import com.qiyeji.real_estate.entity.timeTableKey;
import com.qiyeji.real_estate.service.TimeTableService;
import com.qiyeji.real_estate.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/TimeTable")
@RestController
@ResponseBody
public class TimeTableController {
    @Autowired
    TimeTableService timeTableService;

    @PostMapping("/getAllTimeTable")
    public Result getAllTimeTable() {
        List<timeTable> timeTable = timeTableService.getAllTimeTable();
        if(timeTable==null) {
            return Result.error();
        }
        else {
            return Result.success(timeTable);
        }
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody timeTable timeTable){
        int res=timeTableService.insert(timeTable);
        if(res==0) {
            return Result.error();
        }
        else {
            return Result.success(res);
        }
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody timeTableKey key){
        int res=timeTableService.delete(key);
        if(res==0){
            return Result.error();
        }else{
            return Result.success(res);
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody timeTable timeTable){
        int res=timeTableService.updateByPrimaryKey(timeTable);
        if(res==0){
            return Result.error();
        }else{
            return Result.success(res);
        }
    }

    @PostMapping("/selectById")
    public Result selectById(@RequestBody timeTableKey key){
        timeTable timeTable=timeTableService.selectByPrimaryKey(key);
        if(timeTable==null){
            return Result.error();
        }else{
            return Result.success(timeTable);
        }
    }

    @PostMapping("/fuzzyQuery")
    public Result fuzzyQuery(@RequestBody Map<String,String> params){
        String key = params.get("key");
        List<timeTable> timeTable = timeTableService.fuzzyQuery(key);
        if(timeTable==null) {
            return Result.error();
        }
        else {
            return Result.success(timeTable);
        }
    }
}
